$(function () {
    var me_data = {
        baseConfig: baseConfig,
        user: {}
    };
    var tab_me = Vue.extend({
        template: "#tab_me",
        data: function () {
            return me_data
        },
        methods: {
            go_address: function () {
                api.sys.get_userinfo().then(function (data) {
                    location.href = './user_address.html'
                });

            },
            go_about_me: function () {
                api.sys.get_userinfo().then(function (data) {
                    location.href = './about_me.html'
                });

            },
            go_change_password: function () {
                api.sys.get_userinfo().then(function (data) {
                    location.href = './change_password.html'
                });

            },
            unbind_logout:function () {
                location.href = "/sys/wx_unbind_logout";
            },
            login_wx:function () {
                login_mogu(function () {

                }, "home", null, true);
            }
        }
    });

    var cart_data = {
        baseConfig: baseConfig,
        is_all: false,
        cart_list: [],
        goods_ids: [],
        goods_num: 0
    };
    function checkAll() {
        cart_data.goods_ids.splice(0, cart_data.goods_ids.length);
        if (!cart_data.is_all) {
            cart_data.cart_list.forEach(function (item) {
                if(item.is_active){
                    cart_data.goods_ids.push(item.goods_id);
                }

            });
        }
        cart_data.is_all = !cart_data.is_all;
    }
    var tab_cart = Vue.extend({
        template: "#tab_cart",
        data: function () {
            return cart_data
        },
        methods: {
            price_filter: price_filter,
            cart_pay: function (goods_ids) {
                var cart_list_ids = [];
                $.each(goods_ids, function (index, goods_id) {
                    $.each(cart_data.cart_list, function (index, c_goods) {
                        if(c_goods.goods_id == goods_id){
                            cart_list_ids.push(c_goods.id)
                        }
                    })
                })
                api.lorder.create_order({cart_list:cart_list_ids.join(',')}).then(function (data) {
                    var payorder_id = data.result.id;
                    location.href = './pay.html?payorder_id='+payorder_id
                })
            },
            delete_cart:function (c_goods) {
                Magiclamp.mcConfirm("提示", "从购物删除商品？", null, function () {
                    api.lgoods.delete_carts({cart_id_list: c_goods.id}).then(function (data) {
                        Magiclamp.mcAlert("提示", data.message, null, function () {
                            c_goods.is_active = false;
                            cart_data.is_all = false;
                            checkAll();
                        });

                    });
                });
            },
            go_goods_detail:function (goods_id) {
                location.href = 'goods.html?goods_id='+goods_id
            },
            pop_num: function (c_goods) {

                c_goods.num -= 1;
                var goods_id = c_goods.goods_id;
                api.lgoods.update_cart({goods_id: goods_id, num: c_goods.num}).then(function (data) {
                });
                return false;
            },
            add_num: function (c_goods) {
                c_goods.num += 1;
                var goods_id = c_goods.goods_id;
                api.lgoods.update_cart({goods_id: goods_id, num: c_goods.num}).then(function (data) {
                });
                return false;
            },
            checkAll: function () {
                checkAll();
            },
            price_all: function (goods_ids) {
                var prices = 0;
                $.each(goods_ids, function (index, goods_id) {
                    $.each(cart_data.cart_list, function (index, c_goods) {
                        if (c_goods.goods_id == goods_id) {
                            prices += c_goods.goods__price * c_goods.num
                        }
                    });
                });
                return cart_data.cart_list.prices = prices
            }
        }
    });
    var order_data = {
        baseConfig: baseConfig,
        un_pay_order_list: [],
        goods_order_list: [],
        pay_order_list: [],
        no_page_index:0,
        pay_page_index:0,
        over_page_index:0,
        no_page_count:0,
        pay_page_count:0,
        over_page_count:0,
    };

    function query_un_pay_order_info_list() {
        api.lorder.query_pay_order_info_list({charge_status: '0,1,3',page_index:order_data.no_page_index+1}).then(function (data) {
            // order_data.un_pay_order_list = data.result.list;
            order_data.no_page_index = data.result.page_index;
            order_data.no_page_count = data.result.page_count;
            _(data.result.list).each(function (item) {
                order_data.un_pay_order_list.push(item);
                item['goods_num'] = 0;
                _(item.goods_order_list).each(function (order) {
                    _(order.goods_order_item_list).each(function (goods) {
                        item['goods_num'] += goods.num;

                    });
                });

            });
        });
    }

    function query_pay_order_info_list() {
        api.lorder.query_goods_order_list({status: '1', goods_status: '0,1,2,3',page_index:order_data.pay_page_index+1}).then(function (data) {
            // order_data.goods_order_list = data.result.list;
            order_data.pay_page_index = data.result.page_index;
            order_data.pay_page_count = data.result.page_count;
            _(data.result.list).each(function (item) {
                order_data.goods_order_list.push(item);
                item['goods_num'] = 0;
                _(item.goods_order_item_list).each(function (goods) {
                    item['goods_num'] += goods.num;
                });
            });
        });
    }

    function query_goods_order_list() {
        api.lorder.query_finish_goods_order_list({page_index:order_data.over_page_index+1}).then(function (data) {
            // order_data.pay_order_list = data.result.list;
            order_data.over_page_index = data.result.page_index;
            order_data.over_page_count = data.result.page_count;
            _(data.result.list).each(function (item) {
                order_data.pay_order_list.push(item);
                item['goods_num'] = 0;
                _(item.goods_order_item_list).each(function (goods) {
                    item['goods_num'] += goods.num;
                });
            });
        });
    }

    function query_live_type_list() {
        api.lprogram.query_live_type_list().then(function (data) {
            program_data.live_type_list.push({id:0, name:"推荐", checked:true});
            _(data.result.list).each(function (item) {
                if(item.parent_id>0){
                    program_data.live_type_list.push(item);
                }
            });

        });
    }

    var tab_order = Vue.extend({
        template: "#tab_order",
        data: function () {
            return order_data
        },
        methods: {
            price_filter: price_filter,
            query_un_pay_order_info_list: query_un_pay_order_info_list,
            query_pay_order_info_list: query_pay_order_info_list,
            query_goods_order_list: query_goods_order_list,
            make_goods_order: function (goods_order) {
                api.lorder.confirm_goods_received({goods_order_id: goods_order.id}).then(function (data) {
                    goods_order.goods_status = data.result.goods_status;
                    Magiclamp.mcAlert("提示", data.message);
                });
            },

            open_payorder: function (payorder_id) {
                location.href = 'pay.html?payorder_id=' + payorder_id;
            },
            open_order_info: function (order_id) {
                location.href = 'user_goods_orders.html?goods_order_id=' + order_id;
            },
            delete_payorder: function (payorder) {
                Magiclamp.mcConfirm("提示", "确认删除订单么？", null, function () {
                    api.lorder.delete_order({pay_order_id: payorder.id}).then(function (data) {
                        Magiclamp.mcAlert("提示", data.message);
                        payorder.is_active = false;

                        order_data.un_pay_order_list = _(order_data.un_pay_order_list).filter(function (item) {
                            return item.is_active;
                        });
                    });
                });

            },
            dispute_goods_order: function (goods_order) {
                Magiclamp.mcConfirm("提示", "确认投诉订单么？", null, function () {
                    api.lorder.dispute_goods_order({order_id: goods_order.id}).then(function (data) {
                        Magiclamp.mcAlert("提示", data.message);
                        goods_order.dispute_step = 1
                    })
                });
            },
            shengyudays: function (goods_order) {
                if (goods_order.goods_status == 4 || goods_order.goods_status == 0 || goods_order.goods_status == 1) {
                    return "";
                }
                if (goods_order.dispute_step == 0) {
                    //"received_date" "shipped_date"
                    if (goods_order.goods_status == 2) {
                        var shipped_date = moment();
                        var end_date = moment(goods_order.shipped_date);
                        end_date.add(10, 'days');
                        var days = end_date.diff(shipped_date, 'days');
                        if (days == 0) {
                            return "(剩余：" + end_date.diff(shipped_date, 'hours') + "小时)"
                        } else {
                            return "(剩余：" + days + "天)";
                        }
                    } else if (goods_order.goods_status == 3) {
                        var shipped_date = moment();
                        var end_date = moment(goods_order.received_date);
                        end_date.add(10, 'days');
                        var days = end_date.diff(shipped_date, 'days');
                        if (days == 0) {
                            return "(剩余：" + end_date.diff(shipped_date, 'days') + "小时)"
                        } else {
                            return "(剩余：" + days + "天)";
                        }
                    }

                } else {
                    return "";
                }
            }
        }
    });

    var program_data = {
        baseConfig: baseConfig,
        page_index:0,
        page_count:0,
        program_list: [],
        live_type_list:[],
        search:"",
        program_img: {},
    };
    var tab_program = Vue.extend({
        template: "#tab_program",
        data: function () {
            return program_data;
        },
        methods: {
            timeline_str: function () {
                var t = new Date().getTime();
                t = parseInt(t/1000/60/3);
                return "?t="+t;
            },
            open_program: function (program_id) {
                var phone_type_obj = phone_type();
                if(phone_type_obj.is_wx){
                    if(phone_type_obj.is_ios){
                        location.href = 'play_ios.html?type=ios&program_id='+program_id;
                    }else if(phone_type_obj.is_android){
                        location.href = 'play_android.html?type=android&program_id='+program_id;
                    }else{
                        // 非android 和 ios 手机上的微信浏览器
                    }
                }else{
                    if(phone_type_obj.is_ios){
                        location.href = 'play_ios.html?type=ios&program_id='+program_id;
                    }else if(phone_type_obj.is_android){
                        location.href = 'play_android.html?type=android&program_id='+program_id;
                    }else{
                        // 非 ios 和 android的 系统的其他浏览器
                    }
                }

            },
            search_by_livetype:function (livetype_id) {
                _(program_data.live_type_list).each(function (item) {
                    item.checked=false;
                    if(item.id==livetype_id){
                        item.checked = true;
                    }
                });
                program_data.program_list = [];
                program_data.page_index = 0;
                program_data.page_count = 0;
                program_data.livetype_id = livetype_id;
                var parm = {page_index:program_data.page_index+1, name:program_data.search, livetype_id: program_data.livetype_id}
                if(parm.livetype_id==0){
                    delete parm.livetype_id
                    parm['is_hot'] = true;
                }
                api.lprogram.query_live_program_list(parm).then(function (data) {

                    $.each(data.result.list, function (index, item) {
                        item.current_config__data_json = JSON.parse(item.current_config__data_json);
                        if (item.repeat != '') {
                            item.repeat = JSON.parse(item.repeat);
                            var time_time = '';
                            var time_week = '周';
                            $.each(item.repeat, function (index, time) {
                                time_time = time.startTime + '-' + time.endTime;
                                if (time.week == 1) {
                                    time_week += '一、';
                                } else if (time.week == 2) {
                                    time_week += '二、';
                                } else if (time.week == 3) {
                                    time_week += '三、';
                                } else if (time.week == 4) {
                                    time_week += '四、';
                                } else if (time.week == 5) {
                                    time_week += '五、';
                                } else if (time.week == 6) {
                                    time_week += '六、';
                                } else if (time.week == 7) {
                                    time_week += '日、';
                                }
                            });
                            item.time_repeat = time_time;
                            item.time_week = time_week.slice(0, -1)
                        }

                    });

                    _(data.result.list).each(function (item) {
                        program_data.program_list.push(item);
                    });

                    program_data.page_index = data.result.page_index;
                    program_data.page_count = data.result.page_count;

                });
            },
            search_by_name:function () {
                program_data.program_list = [];
                program_data.page_index = 0;
                program_data.page_count = 0;
                var parm = {page_index:program_data.page_index+1, name:program_data.search, livetype_id: program_data.livetype_id}
                if(parm.livetype_id==0){
                    delete parm.livetype_id
                    parm['is_hot'] = true;
                }
                api.lprogram.query_live_program_list(parm).then(function (data) {

                    $.each(data.result.list, function (index, item) {
                        item.current_config__data_json = JSON.parse(item.current_config__data_json);
                        if (item.repeat != '') {
                            item.repeat = JSON.parse(item.repeat);
                            var time_time = '';
                            var time_week = '周';
                            $.each(item.repeat, function (index, time) {
                                time_time = time.startTime + '-' + time.endTime;
                                if (time.week == 1) {
                                    time_week += '一、';
                                } else if (time.week == 2) {
                                    time_week += '二、';
                                } else if (time.week == 3) {
                                    time_week += '三、';
                                } else if (time.week == 4) {
                                    time_week += '四、';
                                } else if (time.week == 5) {
                                    time_week += '五、';
                                } else if (time.week == 6) {
                                    time_week += '六、';
                                } else if (time.week == 7) {
                                    time_week += '日、';
                                }
                            });
                            item.time_repeat = time_time;
                            item.time_week = time_week.slice(0, -1)
                        }

                    });

                    _(data.result.list).each(function (item) {
                        program_data.program_list.push(item);
                    });

                    program_data.page_index = data.result.page_index;
                    program_data.page_count = data.result.page_count;

                });
            },
            load_more_program:function () {
                var parm = {page_index:program_data.page_index+1, name:program_data.search, livetype_id: program_data.livetype_id}
                if(parm.livetype_id==0){
                    delete parm.livetype_id
                    parm['is_hot'] = true;
                }
                api.lprogram.query_live_program_list(parm).then(function (data) {
                    $.each(data.result.list, function (index, item) {
                        item.current_config__data_json = JSON.parse(item.current_config__data_json);
                        if (item.repeat != '') {
                            item.repeat = JSON.parse(item.repeat);
                            var time_time = '';
                            var time_week = '周';
                            $.each(item.repeat, function (index, time) {
                                time_time = time.startTime + '-' + time.endTime;
                                if (time.week == 1) {
                                    time_week += '一、';
                                } else if (time.week == 2) {
                                    time_week += '二、';
                                } else if (time.week == 3) {
                                    time_week += '三、';
                                } else if (time.week == 4) {
                                    time_week += '四、';
                                } else if (time.week == 5) {
                                    time_week += '五、';
                                } else if (time.week == 6) {
                                    time_week += '六、';
                                } else if (time.week == 7) {
                                    time_week += '日、';
                                }
                            });
                            item.time_repeat = time_time;
                            item.time_week = time_week.slice(0, -1)
                        }

                    });
                    _(data.result.list).each(function (item) {
                        program_data.program_list.push(item);
                    });

                    program_data.page_index = data.result.page_index;
                    program_data.page_count = data.result.page_count;

                });
            },
            shengyudays: function (plan_start_time,live_status) {
                if (live_status == 7) {
                    return "正在直播";
                } else if (!plan_start_time) {
                    return "等待直播";
                } else {
                    var start_time = moment(plan_start_time);
                    var now = moment();
                    if(start_time.isBefore(now)){

                        return "等待直播";
                    }
                    var days = start_time.diff(now, 'days');
                    start_time = start_time.subtract(days, 'days');
                    var hours = start_time.diff(now, 'hours');
                    start_time = start_time.subtract(hours, 'hours');
                    var minutes = start_time.diff(now, 'minutes');
                    if (days == 0) {
                        return "(剩余：" + now.subtract(hours).hours() + "小时)"
                    } else if (days < 0) {
                        return "等待直播";
                    } else {
                        return "(剩余：" + days + "天" + hours + '小时' + minutes + '分)';
                    }
                }

            }
        }
    });
    var home_data = {
        tab_programText: "tab_program",
        tab_cartText: "tab_cart",
        tab_orderText: "tab_order",
        tab_meText: "tab_me",
        currentView: "tab_program",
        active:0
    };
    var home = new Vue({
        el: "#home",
        data: home_data,
        components: {
            tab_program: tab_program,
            tab_cart: tab_cart,
            tab_order: tab_order,
            tab_me: tab_me
        },
        methods: {
            query_un_pay_order_info_list: query_un_pay_order_info_list,
            query_pay_order_info_list: query_pay_order_info_list,
            query_goods_order_list: query_goods_order_list,
            toggleTabs: function (tabText) {
                this.currentView = tabText;
                client_storage("page", tabText);
                this.active = tabText;
                if (tabText == 'tab_order') {

                    // if(!window.mySwipe){
                    setTimeout(function () {
                        var $tags = $('#order-tab').find('a');
                        window.mySwipe = new Swipe(document.getElementById('slider'), {
                            continuous: false,
                            disableScroll: false,
                            stopPropagation: false,
                            callback: function (index) {
                                $tags.eq(index).addClass('tag-on')
                                    .siblings('a').removeClass('tag-on');
                            },
                            transitionEnd: function (index, elem) {
                            }
                        });
                        $tags.eq(0).click(function (e) {
                            mySwipe.slide(0);
                            e.preventDefault();
                        });
                        $tags.eq(1).click(function (e) {
                            mySwipe.slide(1);
                            e.preventDefault();
                        });
                        $tags.eq(2).click(function (e) {
                            mySwipe.slide(2);
                            e.preventDefault();
                        });
                    }, 1000);

                    // }

                }
            },
            go_order_detail: function (payorder_id) {
                location.href = './order-detail.html?payorder_id=' + payorder_id;
            }
        }
    });
    // var goto_app = window.sessionStorage.getItem("goto_app");
    // if(!goto_app){
    //     Magiclamp.mcConfirm("提示", "是否前往APP？", null, function () {
    //         window.location.href = baseConfig.app_download_url;
    //     });
    //     window.sessionStorage.setItem("goto_app", "true");
    // }

    document.getElementsByTagName("BODY")[0].onpageshow = function (event) {
        if(event && !event.persisted){
            return;
        }
        api.lprogram.query_live_program_list({is_hot: true}).then(function (data) {
            $.each(data.result.list, function (index, item) {
                item.current_config__data_json = JSON.parse(item.current_config__data_json);
                if (item.repeat != '') {
                    item.repeat = JSON.parse(item.repeat);
                    var time_time = '';
                    var time_week = '周';
                    $.each(item.repeat, function (index, time) {
                        time_time = time.startTime + '-' + time.endTime;
                        if (time.week == 1) {
                            time_week += '一、';
                        } else if (time.week == 2) {
                            time_week += '二、';
                        } else if (time.week == 3) {
                            time_week += '三、';
                        } else if (time.week == 4) {
                            time_week += '四、';
                        } else if (time.week == 5) {
                            time_week += '五、';
                        } else if (time.week == 6) {
                            time_week += '六、';
                        } else if (time.week == 7) {
                            time_week += '日、';
                        }
                    });
                    item.time_repeat = time_time;
                    item.time_week = time_week.slice(0, -1)
                }

            });
            program_data.program_list = data.result.list;
            program_data.page_index = data.result.page_index;
            program_data.page_count = data.result.page_count;

        });
        query_live_type_list();

        order_data.un_pay_order_list = [];
        order_data.goods_order_list = [];
        order_data.pay_order_list = [];

        login_mogu(function () {
            api.sys.get_userinfo().then(function (data) {
                me_data.user = data.result
            });


            api.lgoods.query_cart_list().then(function (data) {
                cart_data.cart_list = data.result.list

                // tab_cart.checkAll();
                cart_data.goods_ids=[];
                cart_data.cart_list.forEach(function (item) {
                    if(item.is_active){
                        cart_data.goods_ids.push(item.goods_id);
                    }
                });
                cart_data.is_all = true;
            });
            home.query_un_pay_order_info_list();
            home.query_pay_order_info_list();
            home.query_goods_order_list();

        }, "home", null, false);
    };
    document.getElementsByTagName("BODY")[0].onpageshow();


    home_data.sessionid = GetQueryString('sessionid');
    if (home_data.sessionid) {
        home_data.sessionid_q = "sessionid=" + home_data.sessionid;
        client_storage('sessionid', home_data.sessionid);
    }


    // setTimeout(function () {
    //
    // }, 200);
    var current = client_storage("page");
    if(current){
        home.toggleTabs(current);
    }





});


